#!/usr/bin/python
# Operating System Check v0.01
# by William Edwards 07-25-09
# 
# This component checks to see which operating system
# the program is running on and delegates the use of the
# correct modules for the corresponding operating systems.

# Imports the platform module to check what OS the system
# is running.
import platform
import os

# This function determines what OS is running.
def ostype():
	# Checks the system to see if it is running on Linux
	if platform.system() == 'Linux':
		print 'Linux operating system detected.'
		osvar = 'Linux'
		return osvar # Returns the OS variable to the main program
		             # so the program can use the Linux layer to
		             # execute external applications.
	# Checks the system to see if it is running on Windows
	elif platform.system() == 'Windows':
		print 'Windows operating system detected.'
		osvar = 'Windows'
		return osvar # Returns the OS variable to the main program
		             # so the program can use the Windows layer to
		             # execute external applications.
	# If Windows or Linux is not detected, it will throw an error and quit
	else:
		print 'Your OS is currently not supported.'
		quit()

# This function determines if Nmap is installed and tries to install it
# if it is not.
def nmap(ostype):
	installed = False
	while installed != True: # This creates a loop to check if nmap is installed.
		## Linux NMAP Check ##
		# This checks if the system is running linux and if nmap exists.
		if ostype == 'Linux' and os.path.exists('/usr/bin/nmap') == True:
			print 'Nmap is currently installed.'
			installed = True # If nmap is detected, it sets the installed variable
			                 # to true, exiting the loop.
		elif ostype == 'Linux': # This will run if Linux is detected, but nmap is not installed.
			# If it is not installed, it gives the user a choice to install it using apt.
			print 'Nmap is currently not installed.'
			print 'Would you like to install Nmap using apt? (y/n)'
			choice = str(raw_input('Choice: ')) # Prompts the user for a choice
			if choice == 'y':
				os.system('sudo apt-get install -f -y nmap') # If they say yes, it will
				                                             # run apt to install nmap.
				# Once apt has run, it will loop back to check if /usr/bin/nmap exists again
			else: # If they choose no, then the program exits.
				print 'Nmap is required to run Ubuntu Systems Management.'
				print 'Exiting...'
				quit()
		## Windows NMAP Check ##
		# This checks if the system is running Windows and if nmap exists.
		elif ostype == 'Windows' and os.path.exists('nmap-win/nmap.exe') == True:
			print 'Nmap is currently installed.'
			installed = True # If nmap is detected, it exits the loop.
		# If nmap is not present in the local directory, the program will exit.
		elif ostype == 'Windows':
			print 'Nmap is currently not installed.'
			print 'Nmap is required to run Ubuntu Systems Management.'
			print 'Exiting...'
			quit()
		else:
			print 'An unknown error has occured.'
			print 'How did you break this!?' # =P
			quit()

# This function determines if the pexpect module is installed, which is required to pass
# SSH credentials.
def pexpect(ostype):
	installed = False
	while installed != True: # This creates a loop to check if pexpect is installed.
		# This will check to see if pexpect can be loaded.
		try:
			import pexpect
			print 'Pexpect is currently installed.'
			break # breaks the loop if module is installed.
		except ImportError:
			print 'Pexpect module is not installed!'
		## Linux Pexpect Check ##
		# If it is not installed, and they are on a Linux system, it will ask the user
		# if they want to install pexpect using apt.
		if ostype == 'Linux':
			print 'Would you like to install pexpect using apt? (y/n)'
			choice = str(raw_input('Choice: ')) # Prompts the user for a choice
			if choice == 'y':
				os.system('sudo apt-get install -f -y python-pexpect') # Installs pexpect
			else:
				print 'Pexpect is required to run Ubuntu Systems Management.'
				print 'Exiting...'
				quit() # Quits the application if they do not install.
		## Windows Pexpect Check ##
		elif ostype == 'Windows':
			print 'Pexpect is required to run Ubuntu Systems Management.'
			print 'Please install the Pexpect module!'
			print 'Exiting...'
			quit() # If it is not installed, the program will quit.
		else:
			print 'An unknown error has occured.'
			print 'How did you break this!?' # >.<
			quit()

# This function determines if SSH is installed and tries to install it
# if it is not.
def ssh(ostype):
	installed = False
	while installed != True: # This creates a loop to check if SSH is installed.
		## Linux SSH Check ##
		# This checks if the system is running linux and if SSH exists.
		if ostype == 'Linux' and os.path.exists('/usr/bin/ssh') == True:
			print 'SSH is currently installed.'
			installed = True # If SSH is detected, it sets the installed variable
			                 # to true, exiting the loop.
		elif ostype == 'Linux': # This will run if Linux is detected, but SSH is not installed.
			# If it is not installed, it gives the user a choice to install it using apt.
			print 'SSH is currently not installed.'
			print 'Would you like to install SSH using apt? (y/n)'
			choice = str(raw_input('Choice: ')) # Prompts the user for a choice
			if choice == 'y':
				os.system('sudo apt-get install -f -y ssh') # If they say yes, it will
				                                             # run apt to install SSH.
				# Once apt has run, it will loop back to check if /usr/bin/ssh exists again
			else: # If they choose no, then the program exits.
				print 'SSH is required to run Ubuntu Systems Management.'
				print 'Exiting...'
				quit()
		## Windows SSH Check ##
		# This checks if the system is running Windows and if putty.exe exists.
		elif ostype == 'Windows' and os.path.exists('putty-win/putty.exe') == True:
			print 'Putty is currently installed.'
			installed = True # If putty is detected, it exits the loop.
		# If putty.exe is not present in the local directory, the program will exit.
		elif ostype == 'Windows':
			print 'Putty is currently not installed.'
			print 'Putty is required to run Ubuntu Systems Management.'
			print 'Exiting...'
			quit()
		else:
			print 'An unknown error has occured.'
			print 'How did you break this!?' # =P
			quit()
